Skip to content

Migrate from requests to httpx2 - #4560

Merged
r4victor merged 37 commits into
masterfrom
issue_4427_requests_to_httpx2
Nov 30, 2026
Merged

r4victor merged 37 commits into
masterfrom
issue_4427_requests_to_httpx2

Conversation

@r4victor

@r4victor r4victor commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #4427: moves dstack's own HTTP code from requests to httpx2. requests remains only through python-dxf for Docker registries.

  • Add HTTPClient and AsyncHTTPClient base classes with a required timeout, a dstack/<version> User-Agent, and system CA trust with the certifi fallback, and use them for all httpx2 clients.
  • Move the runner and shim client, the backend API clients, the REST plugin, the tokenizer config fetch, the Python API client, and the CLI to httpx2, keeping their timeouts and redirect behavior, and drop requests-unixsocket (http+unix:// server URLs still work).
  • Backend API clients now keep one connection pool per client and are closed when their compute closes and after credentials validation.
  • APIClient retries only connection failures and raises only dstack errors, including for non-JSON 400 and 422 responses.
  • Add close() and context manager support to APIClient and Client.
  • Don't load CA certificates for plain HTTP and Unix socket clients, which cost about 5 ms per runner and shim call on Linux.
  • Close gateway clients together with their connections instead of from __del__, stop closed gateway connections from being used or reopened, clean up gateway connections that fail to open without touching a replacement, and close evicted gateway server clients once their requests finish.
  • Fix non-UTF-8 CloudRift responses, Vast.ai network errors reported as invalid credentials, non-JSON registry manifests causing 500s, and tracebacks from dstack login on request errors and invalid URLs.
  • Replace requests_mock with an http_mock fixture for httpx2 in tests.

Performance

  • Backend API requests reuse connections instead of opening one per request: sequential HTTPS requests were about 4.7x faster in a test (200 ms instead of 940 ms per request, mostly TCP and TLS setup).
  • Runner and shim calls, probes, and gateway and service connections no longer load CA certificates, which took about 5 ms per client on Linux (on the event loop for async clients).

Breaking changes

  • TLS verification uses the OS trust store instead of certifi (certifi only on systems without CA certificates), so REQUESTS_CA_BUNDLE and CURL_CA_BUNDLE no longer apply and SSL_CERT_FILE or SSL_CERT_DIR replace them, and macOS verification may reject some private certificates that OpenSSL accepted.
  • ALL_PROXY with a SOCKS proxy requires the socksio package (pip install "httpx2[socks]") instead of PySocks.t).

APIClient now times out (15s connect, 600s otherwise), follows redirects as before, and wraps all request errors in ClientError. http+unix:// server URLs keep working, so requests-unixsocket is no longer needed.
AsyncClientWrapper closed the client from __del__, which needs a running loop and leaves the close task unreferenced.
Runner and shim clients are created per call, and loading the CA bundle takes about 5 ms on Linux.
Closing a client under an in-flight request can fail it with an error other than httpx2.RequestError, which bypasses failover to other replicas.
@r4victor
r4victor merged commit 22b7e24 into master Nov 30, 2026
37 of 52 checks passed
@r4victor
r4victor deleted the issue_4427_requests_to_httpx2 branch November 30, 2026 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant